package cn.four.hqServer.modules.userregister.service.impl;

import cn.four.hqServer.common.entity.HqCardInfo;
import cn.four.hqServer.common.entity.HqUserInfo;
import cn.four.hqServer.modules.transfer.service.ZnbIHqCardInfoService;
import cn.four.hqServer.modules.transfer.service.ZnbIHqUserInfoService;
import cn.four.hqServer.modules.userregister.dto.UserDto;
import cn.four.hqServer.modules.userregister.mapper.UserMapper;
import cn.four.hqServer.modules.userregister.service.RegisterService;
import cn.four.hqServer.modules.userregister.utils.DcNumberUtil;
import cn.four.hqServer.modules.userregister.utils.GetSalt;
import cn.four.hqServer.modules.userregister.utils.GetTime;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ZnbIHqUserInfoService userInfoService;
    @Autowired
    private ZnbIHqCardInfoService cardInfoService;

    /**
     * 判断用户身份证号是否已经存在。存在返回false，否则返回true
     * @param userId
     * @return
     */
    @Override
    public Boolean selectExistUserByUserId(String userId) {
        String s = userMapper.selectUserByUserId(userId);
        return s == null;
    }

    /**
     * 判断用户电话号码是否已经存在。存在返回false，否则返回true
     * @param number
     * @return
     */
    @Override
    public Boolean selectExistUserByNumber(String number) {
        String s = userMapper.selectUserByNumber(number);
        return s == null;
    }

    /**
     * 新增用户信息
     * @param hqUserInfo
     * @return
     */
    @Override
    public Boolean insertUser(HqUserInfo hqUserInfo) {
        //判断用户是否存在
        Boolean aBoolean = selectExistUserByUserId(hqUserInfo.getUserId());
        if (aBoolean) {
            //获取加密所需要的盐
            String salt = GetSalt.getSalt();
            hqUserInfo.setSalt(salt);
            //获取创建和修改时间
            hqUserInfo.setCreateTime(GetTime.getTime());
            hqUserInfo.setUpdateTime(GetTime.getTime());
            //加密支付密码
            hqUserInfo.setPassword(
                    DigestUtils.md5Hex(
                            hqUserInfo.getPassword() + salt
                    )
            );
            //添加用户信息
            HqUserInfo h = hqUserInfo;
            h.getUserId();
            userInfoService.save(hqUserInfo);
            // 生成银行卡号
            DcNumberUtil numberUtil = DcNumberUtil.getInstance();
            String idNumber = numberUtil.generateDCNumber();
            HqCardInfo hqCardInfo = new HqCardInfo();
            hqCardInfo.setCardId(idNumber);
            hqCardInfo.setUserId(hqUserInfo.getUserId());
            hqCardInfo.setPassword(hqUserInfo.getPassword());
            hqCardInfo.setBalance(new BigDecimal(0.0));
            hqCardInfo.setState(0);
            hqCardInfo.setCreateTime(LocalDateTime.now());
            hqCardInfo.setUpdateTime(LocalDateTime.now());
            cardInfoService.save(hqCardInfo);
            return true;
        }
        return false;
    }
}
